package com.recommendation.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.quartz.SchedulerException;

import com.opensymphony.xwork2.ActionContext;
import com.recommendation.db.Dao;
import com.recommendation.domain.Category;
import com.recommendation.domain.RecommendMode;
import com.recommendation.domain.RecommendScope;
import com.recommendation.domain.SpecialItems;
import com.recommendation.service.RecommendEngine;
import com.recommendation.service.TaobaoService;
import com.recommendation.taobao.TaobaoProxy;
import com.taobao.api.ApiException;
import com.taobao.api.domain.Item;
import com.taobao.api.domain.SellerCat;
import com.taobao.api.response.SellercatsListGetResponse;
import com.taobao.api.response.ShopRemainshowcaseGetResponse;

@Results({
	  @Result(location="recommendation.jsp")
})
public class RecommendationAction extends ActionBase{

	private boolean running;
	private RecommendMode mode;
	private RecommendScope scope;
	private List<Category> categories;
	private List<Item> items;
	private SpecialItems special;
	private List<Item> mustItems;
	private List<Item> notItems;
	private long showcasesCount;
	
	public String execute() throws Exception
	{
		long userId = getUserId();
		String session = getSessionId();
		RecommendEngine engine = new RecommendEngine(userId);
		try {
			running = engine.isStarted() && engine.isRunning();
		} catch (SchedulerException e) {
			error(e);
		}
		mode = Dao.INSTANCE.getRecommendMode(userId);
		if (mode == null)
		{
			mode = new RecommendMode();
		}
		TaobaoService service = new TaobaoService();
		String fields = "num_iid,title,pic_url,price,delist_time,has_showcase";
		scope = Dao.INSTANCE.getRecommendScope(userId);
		if (scope != null)
		{
			items = service.getItems(scope.getItems(), fields, session);
		}
		
		special = Dao.INSTANCE.getSpecialItems(userId);
		if (special != null)
		{
			mustItems = service.getItems(special.getMustItems(), fields, session);
			notItems = service.getItems(special.getNotItems(), fields, session);
			_log.info("not kkkkkkkkk: " + special.getNotKeyword());
		}
		retriveCategories();
		_log.info("items: " + getNumIids(items));
		_log.info("must items: " + getNumIids(mustItems));
		_log.info("not items: " + getNumIids(notItems));
		_log.info("must items count: " + mustItems);
		_log.info("not items: " + notItems);
		ShopRemainshowcaseGetResponse remainRsp = TaobaoProxy.getRemainShowCaseCount(session);
		if (remainRsp.isSuccess())
		{
			setShowcasesCount(remainRsp.getShop().getAllCount());
		}
		else
		{
			error(remainRsp);
		}
		return SUCCESS;
	}
	
	public void setRunning(boolean running) {
		this.running = running;
	}

	public boolean isRunning() {
		return running;
	}
	
	private void retriveCategories() throws ApiException
	{
		Map<String, Object> session = ActionContext.getContext().getSession();
		String nick = (String)session.get(USER);
		SellercatsListGetResponse catRsp = TaobaoProxy.getSellerCategories(nick);
		if (catRsp.isSuccess())
		{
			List<SellerCat> cats = catRsp.getSellerCats();
			List<Category> categories = new ArrayList<Category>();
			if (cats != null)
			{
				for (SellerCat cat : cats)
				{
					Category category = new Category(cat);
					if (scope != null)
					{
						category.setChecked(scope.getSellerCids().contains(String.valueOf(cat.getCid())));
					}
					categories.add(category);
				}
				setCategories(categories);
			}
		}
		else
		{
			error(catRsp);
		}
	}

	public void setCategories(List<Category> categories) {
		this.categories = categories;
	}

	public List<Category> getCategories() {
		return categories;
	}

	public void setScope(RecommendScope scope) {
		this.scope = scope;
	}

	public RecommendScope getScope() {
		return scope;
	}

	public void setItems(List<Item> items) {
		this.items = items;
	}

	public List<Item> getItems() {
		return items;
	}

	public void setSpecial(SpecialItems special) {
		this.special = special;
	}

	public SpecialItems getSpecial() {
		return special;
	}

	public void setMustItems(List<Item> mustItems) {
		this.mustItems = mustItems;
	}

	public List<Item> getMustItems() {
		return mustItems;
	}

	public void setNotItems(List<Item> notItems) {
		this.notItems = notItems;
	}

	public List<Item> getNotItems() {
		return notItems;
	}
	
	private String getNumIids(List<Item> items)
	{
		StringBuilder sb = new StringBuilder();
		if (items != null)
		{
			for (Item item : items)
			{
				sb.append(item.getNumIid()).append(",");
			}
		}
		return sb.toString();
	}

	public void setMode(RecommendMode mode) {
		this.mode = mode;
	}

	public RecommendMode getMode() {
		return mode;
	}

	public void setShowcasesCount(long showcasesCount) {
		this.showcasesCount = showcasesCount;
	}

	public long getShowcasesCount() {
		return showcasesCount;
	}
}
